package org.marsdonne.common.test;

import io.zonky.test.db.postgres.embedded.PgBinaryResolver;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;

public class PostgisBinaryResolver implements PgBinaryResolver {

    private final static String FileNamePattern = "postgresql-%s-postgis-%s-%s-%s.tar.xz";

    private String pgSqlVer;
    private String pgGisVer;

    public PostgisBinaryResolver(String pgSqlVer, String pgGisVer) {
        this.pgSqlVer = pgSqlVer;
        this.pgGisVer = pgGisVer;
    }

    @Override
    public InputStream getPgBinary(String system, String machineHardware) throws IOException {
        ClassPathResource resource = new ClassPathResource(String.format(FileNamePattern, pgSqlVer, pgGisVer, system, machineHardware), Thread.currentThread().getContextClassLoader());
        return resource.getInputStream();
    }
}
